package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.PointsOrderGetsBaseBean;
import com.mycjj.android.bean.PointsOrderGetsBaseBean.PointsOrderGetBean.MyGiftBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class Score_MyGiftActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_my_gift);
		initHeader();
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("我的奖品");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Score_MyGiftActivity.this.finish();
			}
		});
	}

	private ListView listview;
	private TextView tv_name;
	private void init() {
		tv_name = (TextView)findViewById(R.id.tv_name);
		listview = (ListView) findViewById(R.id.listview);
		adapter = new MyGiftAdapter(this);
		listview.setAdapter(adapter);
	}

	private void initData() {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("limit", "100");
		params.put("offset", "0");
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String token = Preferences.getSharedPreferences(this, "user_token", "");
		params.put("token", token);
		request.requestPointsOrderGetsWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						PointsOrderGetsBaseBean baseBean = dataManager
								.getPointsOrderGets();
						if (baseBean != null) {
							if (baseBean.getCode() == 200) {
								if (baseBean.getData() != null) {
									dataList = baseBean.getData().getList();
									int count = 0;
									if(dataList != null && dataList.size() > 0){
										count = dataList.size();
									}
									tv_name.setText("您有"+count+"件奖品");
									adapter.setList(dataList);
								} else {
									Log.e("mark", "baseBean.getData() is null!");
								}
							} else {
								Toast.makeText(
										Score_MyGiftActivity.this,
										baseBean.getMsg(),
										Toast.LENGTH_SHORT).show();
							}
						} else {
							Log.e("mark", "PointsOrderGetsBaseBean is null!");
						}
					}
				});
	}

	private MyGiftAdapter adapter;
	private List<MyGiftBean> dataList;
	public class MyGiftAdapter extends TAdapter<MyGiftBean> {

		public MyGiftAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_score_my_gift_view, null);
				holder = new ViewHolder();
				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				holder.pic = (ImageView) convertView.findViewById(R.id.iv_pic);
				holder.tv_price = (TextView) convertView
						.findViewById(R.id.tv_price);
				holder.tv_desc = (TextView) convertView
						.findViewById(R.id.tv_desc);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			MyGiftBean bean = mList.get(position);
			holder.tv_name.setText(bean.getGoods_name());
			imageLoader.displayImage(bean.getGoods_image(), holder.pic);
			holder.tv_price.setText(String.format("价值%s元", bean.getPrice()));
			int type = Integer.parseInt(bean.getType());
			holder.tv_desc.setText((type == 1 ? "兑换" : "抽奖")+"获得  "+bean.getAdd_time());
			return convertView;
		}

		
		
		class ViewHolder {
			TextView tv_name;
			TextView tv_price;
			TextView tv_desc;
			ImageView pic;
		}

	}
}
